package com.es.jd.service;

import com.alibaba.fastjson.JSON;
import com.es.jd.pojo.Content;
import com.es.jd.util.HtmlParseUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;

@Service
public class ContentService {

  /**
   * 解析网站数据放入 es索引中
   */
  @Resource
  private RestHighLevelClient restHighLevelClient;

  public Boolean parseContent(String keywords) throws Exception{
    List<Content> contents = new HtmlParseUtil().parseJD(keywords);

    //把查询到的数据放入es中
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.timeout("2m");
    for (int i = 0; i < contents.size(); i++) {
      bulkRequest.add(new IndexRequest("jd_goods").source(JSON.toJSONString(contents.get(i)), XContentType.JSON));
    }
    BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    return !bulk.hasFailures();
  }

  // 获取数据
  public List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException{
    if (pageNo <= 1) {
      pageNo = 1;
    }

    //条件搜索
    SearchRequest searchRequest = new SearchRequest("jd_goods");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //分页
    sourceBuilder.from(pageNo);
    sourceBuilder.size(pageSize);
    //模糊匹配
    MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", keyword);
    sourceBuilder.query(matchQueryBuilder);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    //执行搜索
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    //解析结果
    List<Map<String, Object>> list = new ArrayList<>();
    for (SearchHit documentFields : searchResponse.getHits().getHits()) {
      list.add(documentFields.getSourceAsMap());
    }
    return list;
  }

  //搜索数据高亮显示
  public List<Map<String, Object>> searchPageHighlightBuilder(String keyword, int pageNo, int pageSize) throws IOException {
    if (pageNo <= 1) {
      pageNo = 1;
    }

    //条件搜索
    SearchRequest searchRequest = new SearchRequest("jd_goods");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //分页
    sourceBuilder.from(pageNo);
    sourceBuilder.size(pageSize);
    //精准匹配
    MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", keyword);
    sourceBuilder.query(matchQueryBuilder);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

    //高亮
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    highlightBuilder.field("title");
    highlightBuilder.requireFieldMatch(false); //多个高亮显示
    highlightBuilder.preTags("<span style='color:red'>");
    highlightBuilder.postTags("</span>");
    sourceBuilder.highlighter(highlightBuilder);


    //执行搜索
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    //解析结果
    ArrayList<Map<String, Object>> list = new ArrayList<>();
    for (SearchHit hit : searchResponse.getHits().getHits()) {

      Map<String, HighlightField> highlightFields = hit.getHighlightFields();
      HighlightField title = highlightFields.get("title");
      Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //原来的结果!
      //解析高亮的字段，将原来的字段换为我们高亮的字段即可!
      if (title != null) {
        Text[] fragments = title.fragments();
        String n_title = "";
        for (Text text : fragments) {
          n_title += text;
          sourceAsMap.put("title", n_title); //高亮字段替换掉原来的内容即可!
        }
        list.add(sourceAsMap);
      }
    }
    return list;
  }
}
